package com.ziq.zcamp.post

import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.databinding.ObservableList
import androidx.recyclerview.widget.RecyclerView
import com.ziq.base.extension.BindingViewHolder
import com.ziq.base.extension.click
import com.ziq.base.extension.createBindingViewHolder
import com.ziq.base.extension.dp
import com.ziq.zcamp.bean.topics.TopicsInfo
import com.ziq.zcamp.databinding.ItemTopicsLayoutBinding
import com.ziq.zcamp.util.ImageUtil

class TopicsAdapter(val isFromInput :Boolean = true, val apply : ((topics : String)->Unit) = {

}, val createTopics : ((topics : String)->Unit) ={

}, val dataList : ObservableList<TopicsInfo>)
    : RecyclerView.Adapter<BindingViewHolder<ItemTopicsLayoutBinding>>(){

    init {
        setHasStableIds(true)
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): BindingViewHolder<ItemTopicsLayoutBinding> {
        return createBindingViewHolder(parent)
    }

    override fun onBindViewHolder(
        holder: BindingViewHolder<ItemTopicsLayoutBinding>,
        position: Int
    ) {
        val item = dataList[position]
        val binding = holder.binding
        binding.tvTopicsContent.text = item.name

        if (isFromInput){
            val param = binding.tvTopicsContent.layoutParams as ConstraintLayout.LayoutParams
            param.marginStart = 16.dp
            binding.tvTopicsContent.layoutParams = param
        }else{
            val param = binding.tvTopicsContent.layoutParams as ConstraintLayout.LayoutParams
            param.marginStart = 12.dp
            binding.tvTopicsContent.layoutParams = param
        }

        val url =  item?.image?.panel_sub_image?.url?:""
        if (url.isEmpty()){
            binding.ivTopicsImage.visibility = View.GONE
        }else{
            binding.ivTopicsImage.visibility = View.VISIBLE
            ImageUtil.getBitmap(0,  url, 0, 0){ _, bitmap ->
                binding.ivTopicsImage.setImageBitmap(bitmap)
            }
        }

        if (item.needCreate){
            binding.tvCreateTopics.visibility = View.VISIBLE
        }else{
            binding.tvCreateTopics.visibility = View.GONE
        }
        binding.root.click {
            if (!item.needCreate){
            }else{
            }
            apply.invoke(item.name?:"")
        }
        binding.tvCreateTopics.click {
            createTopics.invoke(item.name?:"")
        }

    }
}